/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Administracion;

import java.awt.Color;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author pato
 */
public class Movimientos extends javax.swing.JDialog {

    /**
     * Creates new form Movimientos
     */
    int codigo;
    float fventas=0.0f,freingresos=0.0f,fingresos=0.0f,fajustes=0.0f,fsalidas=0.0f,
            ftotal=0.0f;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    PreparedStatement ps;
    ResultSet rs;
    String fechainicio = "", fechafinal = "";
    public Movimientos(java.awt.Frame parent, boolean modal, int id) {
        super(parent, modal);
        initComponents();
        codigo=id;
        cargarDatos();
        finicial.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                cargarDatos();
            }
        });
        ffinal.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                cargarDatos();
            }
        });
    }
    private void cargarDatos(){
        fechainicio = (finicial.getDate() != null) ? sdf.format(finicial.getDate()) : sdf.format(new Date());
        fechafinal = (ffinal.getDate() != null) ? sdf.format(ffinal.getDate()) : "2050-12-30";
        try {
            ps=local_connection.cn.prepareStatement("select sum(cantidad) from detalleventa join venta on venta.folio=detalleventa.folio "
                    + "where date(fecha)>=? and date(fecha)<=? and id_producto = ?");
            ps.setString(1,fechainicio);
            ps.setString(2, fechafinal);
            ps.setInt(3,codigo);
            rs=ps.executeQuery();
            if(rs.next())
                if(rs.getObject(1)!=null)
                    fventas=rs.getFloat(1);
            ps=local_connection.cn.prepareStatement("select sum(despues-antes) from es_almacen "
                    + "where date(fecha)>=? and date(fecha)<=? and concepto = 'Ajuste' and id_producto = ?");
            ps.setString(1,fechainicio);
            ps.setString(2, fechafinal);
            ps.setInt(3,codigo);
            rs=ps.executeQuery();
            if(rs.next())
                if(rs.getObject(1)!=null)
                    fajustes=rs.getFloat(1);
                 ps=local_connection.cn.prepareStatement("select sum(despues-antes) from es_almacen "
                    + "where date(fecha)>=? and date(fecha)<=? and concepto = 'Ingreso' and id_producto = ?");
            ps.setString(1,fechainicio);
            ps.setString(2, fechafinal);
            ps.setInt(3,codigo);
            rs=ps.executeQuery();
            if(rs.next())
                if(rs.getObject(1)!=null)
                    fingresos=rs.getFloat(1);
                 ps=local_connection.cn.prepareStatement("select sum(despues-antes) from es_almacen "
                    + "where date(fecha)>=? and date(fecha)<=? and concepto = 'reingresos' and id_producto = ?");
            ps.setString(1,fechainicio);
            ps.setString(2, fechafinal);
            ps.setInt(3,codigo);
            rs=ps.executeQuery();
            if(rs.next())
                if(rs.getObject(1)!=null)
                    freingresos=rs.getFloat(1);
                 ps=local_connection.cn.prepareStatement("select sum(despues-antes) from es_almacen "
                    + "where date(fecha)>=? and date(fecha)<=? and concepto = 'Salida' and id_producto = ?");
            ps.setString(1,fechainicio);
            ps.setString(2, fechafinal);
            ps.setInt(3,codigo);
            rs=ps.executeQuery();
            if(rs.next())
                if(rs.getObject(1)!=null)
                    fsalidas=rs.getFloat(1);
            
            ftotal=fventas+fajustes+fingresos+freingresos+fsalidas;
            ventas.setText(String.valueOf(fventas));
            ventas.setForeground((fventas<0.0f)?Color.red:new Color(0,150,0));
            ajustes.setText(String.valueOf(fajustes));
            ajustes.setForeground((fajustes<0.0f)?Color.red:new Color(0,150,0));
            ingresos.setText(String.valueOf(fingresos));
            ingresos.setForeground((fingresos<0.0f)?Color.red:new Color(0,150,0));
            reingresos.setText(String.valueOf(freingresos));
            reingresos.setForeground((freingresos<0.0f)?Color.red:new Color(0,150,0));
            salidas.setText(String.valueOf(fsalidas));
            salidas.setForeground((fsalidas<0.0f)?Color.red:new Color(0,150,0));
            total.setText(String.valueOf(ftotal));
            total.setForeground((ftotal<0.0f)?Color.red:new Color(0,150,0));
            
        } catch (SQLException ex) {
            Logger.getLogger(Movimientos.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        finicial = new com.toedter.calendar.JDateChooser();
        ffinal = new com.toedter.calendar.JDateChooser();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        reingresos = new javax.swing.JLabel();
        salidas = new javax.swing.JLabel();
        ventas = new javax.swing.JLabel();
        ajustes = new javax.swing.JLabel();
        ingresos = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        total = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel6.setText("Inicio");

        jLabel7.setText("Fin");

        reingresos.setFont(new java.awt.Font("Waree", 1, 15)); // NOI18N
        reingresos.setText("jLabel8");

        salidas.setFont(new java.awt.Font("Waree", 1, 15)); // NOI18N
        salidas.setText("jLabel8");

        ventas.setFont(new java.awt.Font("Waree", 1, 15)); // NOI18N
        ventas.setText("v");

        ajustes.setFont(new java.awt.Font("Waree", 1, 15)); // NOI18N
        ajustes.setText("jLabel8");

        ingresos.setFont(new java.awt.Font("Waree", 1, 15)); // NOI18N
        ingresos.setText("jLabel8");

        jLabel8.setFont(new java.awt.Font("Waree", 1, 15)); // NOI18N
        jLabel8.setText("________");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(1, 1, 1)
                                .addComponent(reingresos))
                            .addComponent(salidas)
                            .addComponent(ingresos)
                            .addComponent(ajustes)
                            .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(ventas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(ventas)
                .addGap(8, 8, 8)
                .addComponent(reingresos)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ajustes)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(ingresos)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(salidas)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel8))
        );

        jLabel1.setText("Ventas:");

        jLabel2.setText("Reajustes:");

        jLabel5.setText("Reentradas:");

        jLabel3.setText("Entradas:");

        jLabel4.setText("Salidas:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(8, 8, 8)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addContainerGap())
        );

        jLabel9.setText("Total:");

        total.setFont(new java.awt.Font("Waree", 1, 15)); // NOI18N
        total.setText("jLabel10");

        jButton1.setText("Cerrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(finicial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(ffinal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel6)
                    .addComponent(jLabel7)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 113, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jLabel9))
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(total))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(23, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(total)
                            .addComponent(jLabel9)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(finicial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(ffinal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton1)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
this.dispose();
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * @param args the command line arguments
     */
   

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel ajustes;
    private com.toedter.calendar.JDateChooser ffinal;
    private com.toedter.calendar.JDateChooser finicial;
    private javax.swing.JLabel ingresos;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JLabel reingresos;
    private javax.swing.JLabel salidas;
    private javax.swing.JLabel total;
    private javax.swing.JLabel ventas;
    // End of variables declaration//GEN-END:variables
}
